$(document).ready(function() {

	$('.content .select-custom select').change(function() {
		var value = $(this).val();
		if (value != "") {
			openTable();
		}
	});
	$(".table-content").mCustomScrollbar();

	$('.datetimepicker').datetimepicker({
		controlType: 'select',
		dateFormat: 'dd/mm/y',
		timeFormat: 'HH:mm:ss'
	});
	$('input.group').click(function() {
		var attr = $(this).attr('value');
		if (attr == "Group") {
			$(this).attr("value","Ungroup");
		} else {
			$(this).attr("value","Group");
			
		}
		
		$("#searchHidden").val("");
	});
	
	$('.table-filter .filter select').change(function() {
		var value = $(this).val();
		var items = value.split("|"); 

		$(".table-filter .selector input:text").val("");
		$("#label_1").text(items[1]);
		$("#label_2").text(items[1]);
		
		if(items.length === 3){
		
			if(items[2] === "String" ){
				$(".table-filter .selector input:text").datepicker( "destroy" );
				$(".table-filter .selector input:text").removeClass("hasDatepicker").removeAttr('id');
			
				$(".table-filter .selector input:text").eq(1).val("N/A");
				$(".table-filter .selector input:text").eq(1).hide();
				$("#label_2").hide();		
			}
			else{
				$("#label_1").text("From");
				$("#label_2").text("To");
			
				$(".table-filter .selector input:text").show();
				$(".table-filter .selector label").show();
				
				if(items[2] === "Date"){
					$(this).parents(".table-filter").find(".selector input:text").addClass("datetimepicker");
					
					$('.datetimepicker').datetimepicker({
						controlType: 'select',
						dateFormat: 'dd/mm/y',
						timeFormat: 'HH:mm:ss'
					}); 
				}else{
					$(".table-filter .selector input:text").datepicker( "destroy" );
					$(".table-filter .selector input:text").removeClass("hasDatepicker").removeAttr('id');
				}
			}
		
			$(this).parents(".table-filter").find(".selector").show();
		}else{
			$(this).parents(".table-filter").find(".selector").hide();
		}

		
		$.fancybox.reposition();
	});
	
	resize();
});
function openTable() {
	$(".content").hide();
	$(".table").show();
	var width = $(window).width();
	if (width >= 509 && width <= 636) {
		resizePopup(width-58);
	} else {
		resizePopup(600);
	}
	$.fancybox.reposition();
	$(".table-content").mCustomScrollbar("update");
	//return;
}

function resize(){
	var width = $(window).width();
	if (width >= 509 && width <= 636) {
		resizePopup(width-58);
	} else {
		resizePopup(600);
	}
	$.fancybox.reposition();
	$(".table-content").mCustomScrollbar("update");
}

function closeTable() {
	$(".content").show();
	$(".table").hide();
	resizePopup(400);
	$.fancybox.reposition();
}
function resizePopup(popupWidth) {
	$('.fancybox-wrap').width(popupWidth);
	$('.fancybox-inner').width(popupWidth);
	$('.popup').width(popupWidth);
}